package cn.sczhyt.modules.szhy.controller;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import cn.sczhyt.common.utils.UploadFileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import cn.sczhyt.modules.szhy.entity.MeetingMobileAppEntity;
import cn.sczhyt.modules.szhy.service.MeetingMobileAppService;
import cn.sczhyt.common.utils.PageUtils;
import cn.sczhyt.common.utils.R;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * app安装包管理
 *
 * @author luojialin
 * @email 78357966@qq.com
 * @date 2021-04-14 09:36:23
 */
@RestController
@RequestMapping("szhy/meetingmobileapp")
public class MeetingMobileAppController {
    @Autowired
    private MeetingMobileAppService meetingMobileAppService;

    /**
     * 列表
     */
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params){

        PageUtils page = meetingMobileAppService.queryPage(params);

        return R.ok().put("data", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{appId}")
    public R info(@PathVariable("appId") String appId){
		MeetingMobileAppEntity meetingMobileApp = meetingMobileAppService.getById(appId);

        return R.ok().put("meetingMobileApp", meetingMobileApp);
    }

    /**
     * 最新安装包地址
     */
    @GetMapping("/app_release_latest.apk")
    public R downloadLatestApk(){

        return meetingMobileAppService.getReleaseLatestApk();
    }



    /**
     * app安装包上传
     */
    @RequestMapping("/upload")
    public R upload(@RequestParam("file") MultipartFile file){

        R r = UploadFileUtils.appSave(file);

        return r;
    }

    /**
     * app安装包下载
     */
    @GetMapping("/download")
    public R download(@RequestParam String appId, HttpServletResponse response, HttpServletRequest request) throws UnsupportedEncodingException {

        meetingMobileAppService.download(appId,response,request);

        return R.ok();
    }


    /**
     * 保存
     */
    @PutMapping("/save")
    public R save(@RequestBody MeetingMobileAppEntity meetingMobileApp){

        meetingMobileApp.setInsertTime(new Date());
        meetingMobileApp.setUpdateTime(null);
        System.out.println(meetingMobileApp);
        meetingMobileApp.setDeleted(false);
		meetingMobileAppService.save(meetingMobileApp);

        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public R update(@RequestBody MeetingMobileAppEntity meetingMobileApp){
        meetingMobileApp.setUpdateTime(new Date());
		meetingMobileAppService.updateById(meetingMobileApp);

        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    public R delete(@RequestBody String[] appIds){
		meetingMobileAppService.removeByIds(Arrays.asList(appIds));

        return R.ok();
    }

}
